package 小实验;

public class 树 {
    public static void main(String[] args){
        TreeNode treeRoot = new TreeNode(1);
        treeRoot.leftNode = new TreeNode(2);
        treeRoot.rightNode = new TreeNode(3);
        System.out.println("先序遍历");
        TreeNode.function3(treeRoot);
        System.out.println();
        System.out.println("中序遍历:");
        TreeNode.function1(treeRoot);
        System.out.println();
        System.out.println("后序遍历:");
        TreeNode.function2(treeRoot);
    }
}
class TreeNode{
    Integer value;
    TreeNode leftNode;
    TreeNode rightNode;
    TreeNode fatherNode;

    public TreeNode(Integer value) {
        this.value = value;
    }

    public static void function3(TreeNode t){//先序遍历
        if(t==null){
            return ;
        }else {
            System.out.print(t.value+" ");
            function3(t.leftNode);
            function3(t.rightNode);
        }
    }

    public static void function1(TreeNode treeNode){//左中右遍历
        if(treeNode==null){
            return ;
        }else {
            function1(treeNode.leftNode);
            System.out.print(treeNode.value+" ");
            function1(treeNode.rightNode);
        }
    }
     public static void function2 (TreeNode t){//左右中遍历
        if(t==null){
            return ;
        }else{
            function2(t.leftNode);
            function2(t.rightNode);
            System.out.print(t.value+" ");
        }
     }
}
